package com.tongchengsoft.server.impl;

import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tongchengsoft.bean.BeanUsers;
import com.tongchengsoft.dao.IUsersDao;
import com.tongchengsoft.server.IUsersService;
import com.tongchengsoft.tools.StringTools;

/**
 * 用户 service impl
 * 
 * @author Wang
 * 
 */
@Service
public class UsersServiceImpl implements IUsersService {

	@Autowired
	private IUsersDao userDao;

	@Override
	public boolean add(BeanUsers user) throws Exception {

		if (null == user)

			return false;
		if (StringTools.isEmpty(user.getYhBh()))

			return false;
		if (StringTools.isEmpty(user.getYhXm()))

			return false;
		if (StringTools.isEmpty(user.getYhMm()))

			return false;
		if (null == user.getYhBm() || 0 == user.getYhBm())

			return false;
		user.setIsEnable("N");
		user.setIsDelete(0);
		Integer resultId = userDao.add(user);
		System.out.println("reusltId" + resultId);
		if (0 < resultId)

			return true;
		else

			return false;
	}

	@Override
	public boolean delete(BeanUsers user) throws Exception {

		if (null == user)

			return false;
		if (null == user.getLsId() || 0 == user.getLsId())

			return false;

		HashMap<String, Object> hm = new HashMap<String, Object>();
		hm.put("lsId", user.getLsId());
		if (0 < userDao.delete(hm))

			return true;
		else

			return false;
	}

	@Override
	public boolean update(BeanUsers user) throws Exception {

		if (null == user)

			return false;
		if (null == user.getLsId() || 0 == user.getLsId())

			return false;
		if (StringTools.isEmpty(user.getYhBh()))

			return false;
		
		HashMap<String, Object> hm = new HashMap<String, Object>();
		if (!StringTools.isEmpty(user.getYhXm()))

			hm.put("yhXm", user.getYhXm());
		if (!StringTools.isEmpty(user.getYhMm()))

			hm.put("yhMm", user.getYhMm());
		if (null != user.getYhBm() || 0 < user.getYhBm())

			hm.put("yhBm", user.getYhBm());
		if (!StringTools.isEmpty(user.getIsEnable()))

			hm.put("isEnable", user.getIsEnable());
		hm.put("lsId", user.getLsId());
		hm.put("yhBh", user.getYhBh());
		if (0 < userDao.update(hm))

			return true;
		else

			return false;
	}


	@Override
	public BeanUsers findByLsId(Integer lsId) throws Exception {
		
		if(null == lsId || 0 == lsId)
			
			return null;
		HashMap<String, Object> hm = new HashMap<String, Object>();
		hm.put("lsId", lsId);
		List<BeanUsers> list = find(hm);
		if(null == list || 0 == list.size())
			
			return null;
		return list.get(0);
	}

	@Override
	public BeanUsers findByYhBh(String yhBh) throws Exception {
		
		if(StringTools.isEmpty(yhBh))
			
			return null;
		HashMap<String, Object> hm = new HashMap<String, Object>();
		hm.put("yhBh", yhBh);
		List<BeanUsers> list = find(hm);
		if(null == list || 0 == list.size())
			
			return null;
		return list.get(0);
	}
	
	@Override
	public List<BeanUsers> find(HashMap<String, Object> hm) throws Exception {

		return userDao.find(hm);
	}


}
